<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增用户</div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="图片标题">
        <el-input v-model="form.typename" placeholder="请输入图片标题" style="width: 175px"></el-input>
      </el-form-item><br/>
      <el-form-item label="封面" prop="cover">
        <el-upload
            class="avatar-uploader"
            :action="'http://localhost:9090/type/file/upload'"
            :show-file-list="false"
            :on-success="handleCoverSuccess"
        >
          <img v-if="form.timageurl" :src="form.timageurl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
      <el-button type="warning" @click="reset" size="medium">重置</el-button>

    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'AddUser',
  data() {
    return {
      form: {
        typename:'',
        timageurl:'',
      },
      rules: {

      }
    }
  },
  methods: {
    handleCoverSuccess(res) {
      if (res.code === '200') {
        console.log(res.data)
        // this.$set(this.form, 'cover', res.data)
        this.form.timageurl = res.data
      }
    },
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/type/save', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功')
              this.$refs['ruleForm'].resetFields()
              this.$router.push("/typeList")
            } else {
              this.$notify.error(res.msg)
            }
          })
        }
      })
    },
    reset(){
      this.form = this.$options.data().form
    }
  }
}

</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>